[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
GNU Emacs supports two numeric data types: integers and floating point numbers. Integers are whole numbers such as -3, 0, 7, 13, and 511. Their values are exact. Floating point numbers are numbers with fractional parts, such as -4.5, 0.0, or 2.71828. They can also be expressed in an exponential notation as well: thus, 1.5e2 equals 150; in this example, ‘e2’ stands for ten to the second power, and is multiplied by 1.5. Floating point values are not exact; they have a fixed, limited amount of precision.
Support for floating point numbers is a new feature in Emacs 19, and it is controlled by a separate compilation option, so you may encounter a site where Emacs does not support them.
1.1 Integer Basics | Representation and range of integers. | |
1.2 Floating Point Basics | Representation and range of floating point. | |
1.3 Type Predicates for Numbers | Testing for numbers. | |
1.4 Comparison of Numbers | Equality and inequality predicates. | |
1.5 Numeric Conversions | Converting float to integer and vice versa. | |
1.6 Arithmetic Operations | How to add, subtract, multiply and divide. | |
1.7 Bitwise Operations on Integers | Logical and, or, not, shifting. | |
1.8 Transcendental Functions | Trig, exponential and logarithmic functions. | |
1.9 Random Numbers | Obtaining random integers, predictable or not. |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The range of values for an integer depends on the machine. The range is -8388608 to 8388607 (24 bits; i.e., to ) on most machines, but on others it is -16777216 to 16777215 (25 bits), or -33554432 to 33554431 (26 bits). All of the examples shown below assume an integer has 24 bits.
The Lisp reader reads numbers as a sequence of digits with an optional sign.
1 ; The integer 1. +1 ; Also the integer 1. -1 ; The integer -1. 16777217 ; Also the integer 1, due to overflow. 0 ; The number 0. -0 ; The number 0. 1. ; The integer 1.
To understand how various functions work on integers, especially the bitwise operators (see section Bitwise Operations on Integers), it is often helpful to view the numbers in their binary form.
In 24 bit binary, the decimal integer 5 looks like this:
0000 0000 0000 0000 0000 0101
(We have inserted spaces between groups of 4 bits, and two spaces between groups of 8 bits, to make the binary integer easier to read.)
The integer -1 looks like this:
1111 1111 1111 1111 1111 1111
-1 is represented as 24 ones. (This is called two’s complement notation.)
The negative integer, -5, is creating by subtracting 4 from -1. In binary, the decimal integer 4 is 100. Consequently, -5 looks like this:
1111 1111 1111 1111 1111 1011
In this implementation, the largest 24 bit binary integer is the decimal integer 8,388,607. In binary, this number looks like this:
0111 1111 1111 1111 1111 1111
Since the arithmetic functions do not check whether integers go outside their range, when you add 1 to 8,388,607, the value is negative integer -8,388,608:
(+ 1 8388607) ⇒ -8388608 ⇒ 1000 0000 0000 0000 0000 0000
Many of the following functions accept markers for arguments as well as integers. (@xref{Markers}.) More precisely, the actual parameters to such functions may be either integers or markers, which is why we often give these parameters the name int-or-marker. When the actual parameter is a marker, the position value of the marker is used and the buffer of the marker is ignored.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Emacs version 19 supports floating point numbers, if compiled with the
macro LISP_FLOAT_TYPE
defined. The precise range of floating
point numbers is machine-specific; it is the same as the range of the C
data type double
on the machine in question.
The printed representation for floating point numbers requires either a decimal point (with at least one digit following), an exponent, or both. For example, ‘1500.0’, ‘15e2’, ‘15.0e2’, ‘1.5e3’, and ‘.15e4’ are five ways of writing a floating point number whose value is 1500. They are all equivalent. You can also use a minus sign to write negative floating point numbers, as in ‘-1.0’.
You can use logb
to extract the binary exponent of a floating
point number (or estimate the logarithm of an integer):
This function returns the binary exponent of number. More precisely, the value is the logarithm of number base 2, rounded down to an integer.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The functions in this section test whether the argument is a number or
whether it is a certain sort of number. The functions integerp
and floatp
can take any type of Lisp object as argument (the
predicates would not be of much use otherwise); but the zerop
predicate requires a number as its argument. See also
integer-or-marker-p
and number-or-marker-p
, in
@ref{Predicates on Markers}.
This predicate tests whether its argument is a floating point
number and returns t
if so, nil
otherwise.
floatp
does not exist in Emacs versions 18 and earlier.
This predicate tests whether its argument is an integer, and returns
t
if so, nil
otherwise.
This predicate tests whether its argument is a number (either integer or
floating point), and returns t
if so, nil
otherwise.
The natnump
predicate (whose name comes from the phrase
“natural-number-p”) tests to see whether its argument is a nonnegative
integer, and returns t
if so, nil
otherwise. 0 is
considered non-negative.
Markers are not converted to integers, hence natnump
of a marker
is always nil
.
People have pointed out that this function is misnamed, because the term “natural number” is usually understood as excluding zero. We are open to suggestions for a better name to use in a future version.
This predicate tests whether its argument is zero, and returns t
if so, nil
otherwise. The argument must be a number.
These two forms are equivalent: (zerop x) ≡ (= x 0)
.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Floating point numbers in Emacs Lisp actually take up storage, and
there can be many distinct floating point number objects with the same
numeric value. If you use eq
to compare them, then you test
whether two values are the same object. If you want to compare
just the numeric values, use =
.
If you use eq
to compare two integers, it always returns
t
if they have the same value. This is sometimes useful, because
eq
accepts arguments of any type and never causes an error,
whereas =
signals an error if the arguments are not numbers or
markers. However, it is a good idea to use =
if you can, even
for comparing integers, just in case we change the representation of
integers in a future Emacs version.
There is another wrinkle: because floating point arithmetic is not exact, it is often a bad idea to check for equality of two floating point values. Usually it is better to test for approximate equality. Here’s a function to do this:
(defvar fuzz-factor 1.0e-6) (defun approx-equal (x y) (< (/ (abs (- x y)) (max (abs x) (abs y))) fuzz-factor))
Common Lisp note: because of the way numbers are implemented in Common Lisp, you generally need to use ‘
=
’ to test for equality between numbers of any kind.
This function tests whether its arguments are the same number, and
returns t
if so, nil
otherwise.
This function tests whether its arguments are not the same number, and
returns t
if so, nil
otherwise.
This function tests whether its first argument is strictly less than
its second argument. It returns t
if so, nil
otherwise.
This function tests whether its first argument is less than or equal
to its second argument. It returns t
if so, nil
otherwise.
This function tests whether its first argument is strictly greater
than its second argument. It returns t
if so, nil
otherwise.
This function tests whether its first argument is greater than or
equal to its second argument. It returns t
if so, nil
otherwise.
This function returns the largest of its arguments.
(max 20) ⇒ 20 (max 1 2) ⇒ 2 (max 1 3 2) ⇒ 3
This function returns the smallest of its arguments.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To convert an integer to floating point, use the function float
.
This returns number converted to floating point.
If number is already a floating point number, float
returns
it unchanged.
There are four functions to convert floating point numbers to integers; they differ in how they round. You can call these functions with an integer argument also; if you do, they return it without change.
This returns number, converted to an integer by rounding towards zero.
This returns number, converted to an integer by rounding downward (towards negative infinity).
This returns number, converted to an integer by rounding upward (towards positive infinity).
This returns number, converted to an integer by rounding towards the nearest integer.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Emacs Lisp provides the traditional four arithmetic operations: addition, subtraction, multiplication, and division. A remainder function supplements the (integer) division function. The functions to add or subtract 1 are provided because they are traditional in Lisp and commonly used.
All of these functions except %
return a floating point value
if any argument is floating.
It is important to note that in GNU Emacs Lisp, arithmetic functions
do not check for overflow. Thus (1+ 8388607)
may equal
-8388608, depending on your hardware.
This function returns number-or-marker plus 1. For example,
(setq foo 4) ⇒ 4 (1+ foo) ⇒ 5
This function is not analogous to the C operator ++
—it does
not increment a variable. It just computes a sum. Thus,
foo ⇒ 4
If you want to increment the variable, you must use setq
,
like this:
(setq foo (1+ foo)) ⇒ 5
This function returns number-or-marker minus 1.
This returns the absolute value of number.
This function adds its arguments together. When given no arguments,
+
returns 0. It does not check for overflow.
(+) ⇒ 0 (+ 1) ⇒ 1 (+ 1 2 3 4) ⇒ 10
The -
function serves two purposes: negation and subtraction.
When -
has a single argument, the value is the negative of the
argument. When there are multiple arguments, each of the
other-numbers-or-markers is subtracted from number-or-marker,
cumulatively. If there are no arguments, the result is 0. This
function does not check for overflow.
(- 10 1 2 3 4) ⇒ 0 (- 10) ⇒ -10 (-) ⇒ 0
This function multiplies its arguments together, and returns the
product. When given no arguments, *
returns 1. It does
not check for overflow.
(*) ⇒ 1 (* 1) ⇒ 1 (* 1 2 3 4) ⇒ 24
This function divides dividend by divisors and returns the quotient. If there are additional arguments divisors, then dividend is divided by each divisor in turn. Each argument may be a number or a marker.
If all the arguments are integers, then the result is an integer too.
This means the result has to be rounded. On most machines, the result
is rounded towards zero after each division, but some machines may round
differently with negative arguments. This is because the Lisp function
/
is implemented using the C division operator, which has the
same possibility for machine-dependent rounding. As a practical matter,
all known machines round in the standard fashion.
If you divide by 0, an arith-error
error is signaled.
(@xref{Errors}.)
(/ 6 2) ⇒ 3 (/ 5 2) ⇒ 2 (/ 25 3 2) ⇒ 4 (/ -17 6) ⇒ -2
Since the division operator in Emacs Lisp is implemented using the
division operator in C, the result of dividing negative numbers may in
principle vary from machine to machine, depending on how they round the
result. Thus, the result of (/ -17 6)
could be -3 on some
machines. In practice, nearly all machines round the quotient towards
0.
This function returns the value of dividend modulo divisor; in other words, the integer remainder after division of dividend by divisor. The sign of the result is the sign of dividend. The sign of divisor is ignored. The arguments must be integers.
For negative arguments, the value is in principle machine-dependent since the quotient is; but in practice, all known machines behave alike.
An arith-error
results if divisor is 0.
(% 9 4) ⇒ 1 (% -9 4) ⇒ -1 (% 9 -4) ⇒ 1 (% -9 -4) ⇒ -1
For any two numbers dividend and divisor,
(+ (% dividend divisor) (* (/ dividend divisor) divisor))
always equals dividend.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In a computer, an integer is represented as a binary number, a sequence of bits (digits which are either zero or one). A bitwise operation acts on the individual bits of such a sequence. For example, shifting moves the whole sequence left or right one or more places, reproducing the same pattern “moved over”.
The bitwise operations in Emacs Lisp apply only to integers.
lsh
, which is an abbreviation for logical shift, shifts the
bits in integer1 to the left count places, or to the
right if count is negative. If count is negative,
lsh
shifts zeros into the most-significant bit, producing a
positive result even if integer1 is negative. Contrast this with
ash
, below.
Thus, the decimal number 5 is the binary number 00000101. Shifted once to the left, with a zero put in the one’s place, the number becomes 00001010, decimal 10.
Here are two examples of shifting the pattern of bits one place to the
left. Since the contents of the rightmost place has been moved one
place to the left, a value has to be inserted into the rightmost place.
With lsh
, a zero is placed into the rightmost place. (These
examples show only the low-order eight bits of the binary pattern; the
rest are all zero.)
(lsh 5 1) ⇒ 10 ;; Decimal 5 becomes decimal 10. 00000101 ⇒ 00001010 (lsh 7 1) ⇒ 14 ;; Decimal 7 becomes decimal 14. 00000111 ⇒ 00001110
As the examples illustrate, shifting the pattern of bits one place to the left produces a number that is twice the value of the previous number.
Note, however that functions do not check for overflow, and a returned value may be negative (and in any case, no more than a 24 bit value) when an integer is sufficiently left shifted.
For example, left shifting 8,388,607 produces -2:
(lsh 8388607 1) ; left shift
⇒ -2
In binary, in the 24 bit implementation, the numbers looks like this:
;; Decimal 8,388,607
0111 1111 1111 1111 1111 1111
which becomes the following when left shifted:
;; Decimal -2
1111 1111 1111 1111 1111 1110
Shifting the pattern of bits two places to the left produces results like this (with 8-bit binary numbers):
(lsh 3 2)
⇒ 12
;; Decimal 3 becomes decimal 12.
00000011 ⇒ 00001100
On the other hand, shifting the pattern of bits one place to the right looks like this:
(lsh 6 -1) ⇒ 3 ;; Decimal 6 becomes decimal 3. 00000110 ⇒ 00000011 (lsh 5 -1) ⇒ 2 ;; Decimal 5 becomes decimal 2. 00000101 ⇒ 00000010
As the example illustrates, shifting the pattern of bits one place to the right divides the value of the binary number by two, rounding downward.
ash
(arithmetic shift) shifts the bits in integer1
to the left count places, or to the right if count
is negative.
ash
gives the same results as lsh
except when
integer1 and count are both negative. In that case,
ash
puts a one in the leftmost position, while lsh
puts
a zero in the leftmost position.
Thus, with ash
, shifting the pattern of bits one place to the right
looks like this:
(ash -6 -1) ⇒ -3 ;; Decimal -6 ;; becomes decimal -3. 1111 1111 1111 1111 1111 1010 ⇒ 1111 1111 1111 1111 1111 1101
In contrast, shifting the pattern of bits one place to the right with
lsh
looks like this:
(lsh -6 -1) ⇒ 8388605 ;; Decimal -6 ;; becomes decimal 8,388,605. 1111 1111 1111 1111 1111 1010 ⇒ 0111 1111 1111 1111 1111 1101
In this case, the 1 in the leftmost position is shifted one place to the right, and a zero is shifted into the leftmost position.
Here are other examples:
; 24-bit binary values (lsh 5 2) ; 5 = 0000 0000 0000 0000 0000 0101 ⇒ 20 ; 20 = 0000 0000 0000 0000 0001 0100
(ash 5 2) ⇒ 20 (lsh -5 2) ; -5 = 1111 1111 1111 1111 1111 1011 ⇒ -20 ; -20 = 1111 1111 1111 1111 1110 1100 (ash -5 2) ⇒ -20
(lsh 5 -2) ; 5 = 0000 0000 0000 0000 0000 0101 ⇒ 1 ; 1 = 0000 0000 0000 0000 0000 0001
(ash 5 -2) ⇒ 1
(lsh -5 -2) ; -5 = 1111 1111 1111 1111 1111 1011 ⇒ 4194302 ; 0011 1111 1111 1111 1111 1110
(ash -5 -2) ; -5 = 1111 1111 1111 1111 1111 1011 ⇒ -2 ; -2 = 1111 1111 1111 1111 1111 1110
This function returns the “logical and” of the arguments: the nth bit is set in the result if, and only if, the nth bit is set in all the arguments. (“Set” means that the value of the bit is 1 rather than 0.)
For example, using 4-bit binary numbers, the “logical and” of 13 and 12 is 12: 1101 combined with 1100 produces 1100.
In both the binary numbers, the leftmost two bits are set (i.e., they are 1’s), so the leftmost two bits of the returned value are set. However, for the rightmost two bits, each is zero in at least one of the arguments, so the rightmost two bits of the returned value are 0’s.
Therefore,
(logand 13 12) ⇒ 12
If logand
is not passed any argument, it returns a value of
-1. This number is an identity element for logand
because its binary representation consists entirely of ones. If
logand
is passed just one argument, it returns that argument.
; 24-bit binary values (logand 14 13) ; 14 = 0000 0000 0000 0000 0000 1110 ; 13 = 0000 0000 0000 0000 0000 1101 ⇒ 12 ; 12 = 0000 0000 0000 0000 0000 1100
(logand 14 13 4) ; 14 = 0000 0000 0000 0000 0000 1110 ; 13 = 0000 0000 0000 0000 0000 1101 ; 4 = 0000 0000 0000 0000 0000 0100 ⇒ 4 ; 4 = 0000 0000 0000 0000 0000 0100
(logand)
⇒ -1 ; -1 = 1111 1111 1111 1111 1111 1111
This function returns the “inclusive or” of its arguments: the nth bit
is set in the result if, and only if, the nth bit is set in at least
one of the arguments. If there are no arguments, the result is zero,
which is an identity element for this operation. If logior
is
passed just one argument, it returns that argument.
; 24-bit binary values (logior 12 5) ; 12 = 0000 0000 0000 0000 0000 1100 ; 5 = 0000 0000 0000 0000 0000 0101 ⇒ 13 ; 13 = 0000 0000 0000 0000 0000 1101
(logior 12 5 7) ; 12 = 0000 0000 0000 0000 0000 1100 ; 5 = 0000 0000 0000 0000 0000 0101 ; 7 = 0000 0000 0000 0000 0000 0111 ⇒ 15 ; 15 = 0000 0000 0000 0000 0000 1111
This function returns the “exclusive or” of its arguments: the
nth bit is set in the result if, and only if, the nth bit
is set in an odd number of the arguments. If there are no arguments,
the result is 0. If logxor
is passed just one argument, it returns
that argument.
; 24-bit binary values (logxor 12 5) ; 12 = 0000 0000 0000 0000 0000 1100 ; 5 = 0000 0000 0000 0000 0000 0101 ⇒ 9 ; 9 = 0000 0000 0000 0000 0000 1001
(logxor 12 5 7) ; 12 = 0000 0000 0000 0000 0000 1100 ; 5 = 0000 0000 0000 0000 0000 0101 ; 7 = 0000 0000 0000 0000 0000 0111 ⇒ 14 ; 14 = 0000 0000 0000 0000 0000 1110
This function returns the logical complement of its argument: the nth bit is one in the result if, and only if, the nth bit is zero in integer, and vice-versa.
;; 5 = 0000 0000 0000 0000 0000 0101 ;; becomes ;; -6 = 1111 1111 1111 1111 1111 1010 (lognot 5) ⇒ -6
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These mathematical functions are available if floating point is supported. They allow integers as well as floating point numbers as arguments.
These are the ordinary trigonometric functions, with argument measured in radians.
The value of (asin arg)
is a number between - pi / 2
and pi / 2 (inclusive) whose sine is arg; if, however, arg
is out of range (outside [-1, 1]), then the result is a NaN.
The value of (acos arg)
is a number between 0 and pi
(inclusive) whose cosine is arg; if, however, arg
is out of range (outside [-1, 1]), then the result is a NaN.
The value of (atan arg)
is a number between - pi / 2
and pi / 2 (exclusive) whose tangent is arg.
This is the exponential function; it returns e to the power arg.
This function returns the logarithm of arg, with base base. If you don’t specify base, the base e is used. If arg is negative, the result is a NaN.
This function returns the logarithm of arg, with base 10. If arg is negative, the result is a NaN.
This function returns x raised to power y.
This returns the square root of arg.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In a computer, a series of pseudo-random numbers is generated in a deterministic fashion. The numbers are not truly random, but they have certain properties that mimic a random series. For example, all possible values occur equally often in a pseudo-random series.
In Emacs, pseudo-random numbers are generated from a “seed” number.
Starting from any given seed, the random
function always
generates the same sequence of numbers. Emacs always starts with the
same seed value, so the sequence of values of random
is actually
the same in each Emacs run! For example, in one operating system, the
first call to (random)
after you start Emacs always returns
-1457731, and the second one always returns -7692030. This is helpful
for debugging.
If you want truly unpredictable random numbers, execute (random
t)
. This chooses a new seed based on the current time of day and on
Emacs’ process ID number.
This function returns a pseudo-random integer. When called more than once, it returns a series of pseudo-random integers.
If limit is nil
, then the value may in principle be any
integer. If limit is a positive integer, the value is chosen to
be nonnegative and less than limit (only in Emacs 19).
If limit is t
, it means to choose a new seed based on the
current time of day and on Emacs’s process ID number.
On some machines, any integer representable in Lisp may be the result
of random
. On other machines, the result can never be larger
than a certain maximum or less than a certain (negative) minimum.
[Top] | [Contents] | [Index] | [ ? ] |
This document was generated on January 16, 2023 using texi2html 5.0.
The buttons in the navigation panels have the following meaning:
Button | Name | Go to | From 1.2.3 go to |
---|---|---|---|
[ << ] | FastBack | Beginning of this chapter or previous chapter | 1 |
[ < ] | Back | Previous section in reading order | 1.2.2 |
[ Up ] | Up | Up section | 1.2 |
[ > ] | Forward | Next section in reading order | 1.2.4 |
[ >> ] | FastForward | Next chapter | 2 |
[Top] | Top | Cover (top) of document | |
[Contents] | Contents | Table of contents | |
[Index] | Index | Index | |
[ ? ] | About | About (help) |
where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:
This document was generated on January 16, 2023 using texi2html 5.0.